<?php
/**
 * created by zzy
 * date: 2017/11/21 10:39
 */

namespace App\Http\Admin\System\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
//use App\Eloquent\Ygt\AdminRole;
use App\Http\Admin\System\Models\AdminRole;
use App\Eloquent\Ygt\AdminUser;
use App\Eloquent\Ygt\AdminRoleUser;
use App\Http\Admin\Company\Models\Company;

class AdminuserController extends Controller
{
    public function index(){}
    public function lists(){

        if (request()->isMethod('post')) {

            $pageSize       = request('pageSize', 30);
            $pageCurrent    = request('pageCurrent', 1);
            $offset         = ($pageCurrent - 1) * $pageSize;

            $where          = [];
            $fileds         = 'admin_user.*,a.admin_role_id as role_id';
            $join           = [
                ['table'=>'admin_role_user as a', 'field_l'=>'a.admin_user_id', 'field_c'=>'=', 'field_r'=>'admin_user.id'],
            ];
            $lists          = AdminUser::getList( $where,$fileds,$pageSize,$offset,'','',$join )->toArray();
            $count          = AdminUser::getCount($where);
            $data           = ['totalRow'=>$count,'pageCurrent'=>$pageCurrent,'list'=>$lists];
            $data_json      = json_encode($data);
            return $data_json;
        } else {
            $company_json   = Company::getJsonList();
            $role_json      = AdminRole::getJsonList();
            return $this->view('lists', compact('company_json','role_json'));
        }
    }
    public function edit(){

        $ftype              = request( 'ftype');

        $info               = [];
        $role_id            = 0;
        switch( $ftype ){
            case 1:
                //1  来源于角色用户列表页面
                $admin_user_id      = request( 'id' );
                $info               = AdminUser::getInfo( ['id'=>$admin_user_id] );
                $role_info          = AdminRoleUser::getInfo( ['admin_user_id'=>$admin_user_id] );
                $role_id            = $role_info->admin_role_id;
                $company_id         = $info->company_id;
                if( $info ){
                    $info           = $info->toArray();
                }
                break;
            case 2:
                //2  来源于企业列表页面
                $role_id            = 2;
                $company_id         = request( 'company_id', 0 );
                $info               = AdminUser::getInfo( ['company_id'=>$company_id] );
                if( $info ){
                    $info           = $info->toArray();
                }
                break;
            default:

        }
        $role_list          = AdminRole::getList('');
        return $this->view('edit', compact('info','company_id','ftype', 'role_list','role_id') );
    }
    public function update(){

        $user_id            = request( 'id' );
        $old_password       = request( 'old_password', '' );
        $role_id            = request( 'role_id' );
        $company_id         = request( 'company_id' );
        $username           = request( 'username' );
        $password           = request( 'password' );
        $password2          = request( 'password2' );

        if( !$company_id || !$username || !$password || !$password2 ){
            return $this->ajaxFail('数据不全');
        }
        if( $password != $password2 ){
            return $this->ajaxFail('两次密码不一样');
        }

        $password_bcrypt    = bcrypt( $password );
        $data               = [
            'username'=>$username,'password'=>$password_bcrypt, 'company_id'=>$company_id
        ];
        if ( $user_id ) {
            $where          = ['id'=>$user_id];
            $info           = AdminUser::getInfo($where);
            if( $old_password ){
                $info_password          = $info->password;
                //$old_password_bcrypt    = bcrypt( $old_password );
                if (!\Hash::check($old_password, $info_password)){
                    return $this->ajaxFail('旧密码错误');
                }
            } else {
                return $this->ajaxFail('旧密码错误x');
            }
            $where          = ['id'=>$user_id];
            $result         = AdminUser::updateOneData($where, $data);
            $where          = ['admin_user_id'=>$user_id];
            $data           = ['admin_role_id'=>$role_id];
            AdminRoleUser::updateOneData($where,$data);
        } else {
            $admin_user_id  = AdminUser::insertOneData($data,'id');
            $result         = $admin_user_id ? true : false;
            if( $admin_user_id ){
                $data       = ['admin_role_id'=>$role_id, 'admin_user_id'=>$admin_user_id];
                $result     = AdminRoleUser::insertOneData($data);
            }
        }

        if ($result) {
            return $this->ajaxSuccess('成功');
        } else {
            return $this->ajaxFail('失败');
        }
    }
    public function delete(){

    }
}
